package com.lanou.horo.zhuizhui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.adapter.RecentComicsAdapter;
import com.lanou.horo.zhuizhui.entity.RecentComicsItem;
import com.lanou.horo.zhuizhui.utility.recyclerView.SpacesItemDecoration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/11/2.
 */
public class RecentComicsActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecentComicsAdapter adapter;
    private RequestQueue queue;
    private String url;
    private TextView searchTitle;
    private ImageView sbackBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comics_recent);
        recyclerView = (RecyclerView) findViewById(R.id.recentComicsRecyclerView);
        adapter = new RecentComicsAdapter(this);
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        int spacingInPixels = 8;
        recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
        searchTitle = (TextView) findViewById(R.id.searchTitle);
        sbackBtn = (ImageView) findViewById(R.id.sbackBtn);
        sbackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        int pageId = intent.getIntExtra("id", 0);
        switch (pageId) {
            case 39:   //更新中
                url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=1446448182419" +
                        "&param=%7B%22id%22%3A%2239%22%2C%22v%22%3A1%2C%22t%22%3A0%2C%22count" +
                        "%22%3A18%2C%22page%22%3A1%7D&sig=3db7d3da996d8b6e35bec5e6eabfbf35";
                break;
            case 38:   //排行榜
                url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=1446448314275" +
                        "&param=%7B%22id%22%3A%2238%22%2C%22v%22%3A1%2C%22t%22%3A0%2C%22count" +
                        "%22%3A18%2C%22page%22%3A1%7D&sig=608e26dd22ea50c7121d8222e363ffd4";
                break;

            case 116:  //已完结
                url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=1446554346340" +
                        "&param=%7B%22id%22%3A%22116%22%2C%22v%22%3A1%2C%22t%22%3A0%2C%22count" +
                        "%22%3A18%2C%22page%22%3A1%7D&sig=d6021076bc7690ee0e91c83c1e93b077";
                break;
            case 1047:  //新收录
                url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=1446554480605" +
                        "&param=%7B%22id%22%3A%221047%22%2C%22v%22%3A1%2C%22t%22%3A0%2C%22count" +
                        "%22%3A18%2C%22page%22%3A1%7D&sig=c815c6939ced1c5c7abe84fcdd8c8d02";
                break;
        }

        queue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                RecentComicsItem rc = gson.fromJson(response, RecentComicsItem.class);
                if (rc != null) {
                    List<RecentComicsItem.ComicsEntity> comicsEntities = rc.getComics();
                    if (comicsEntities != null && comicsEntities.size() > 0) {
                        adapter.addComicsList(comicsEntities);
                    }
                    RecentComicsItem.ThemeEntity entity = rc.getTheme();
                    if (entity != null) {
                        adapter.addTheme(entity);
                        searchTitle.setText(entity.getName());
                    }
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        queue.add(request);
    }


}
